package com.zhou.aspect;

import com.zhou.annotation.Encryption;
import com.zhou.base.JsonReturnObject;
import com.zhou.util.DesUtil;
import com.zhou.util.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.logging.Logger;

@Component
@Aspect
public class ControllerAspect {

    @Around("execution(* com.zhou.controller..*.*(..))")
    public Object cutPoint(ProceedingJoinPoint point){
        try {
            Object resultObj = point.proceed();
            String resultStr;
            if(resultObj instanceof String){
                resultStr = (String)resultObj;
            }else{
                resultStr = JsonUtil.getGson().toJson(new JsonReturnObject(null, true, resultObj));
                if(point.getTarget().getClass().getAnnotation(Encryption.class) != null){
                    resultStr = DesUtil.encrypt(resultStr);
                }
            }
            return resultStr;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return JsonUtil.getGson().toJson(new JsonReturnObject(throwable.getMessage(), false, null));
        }
    }
}
